﻿// app/service/user.js
const Service = require('egg').Service;
const sha1 = require("sha1")
const GetDistance = require("../bin/GetDistance")
const APPID = "wx1c2dca946df3d61c"
const SECRET = "c4f1bf90411c96243aa3bc3eaf9f67d3"



class User extends Service {

    async sellerPhoneLogin(encode, phoneNumber,code) {

       let decode = sha1(code + phoneNumber + "wang_user")
         
        if (decode != encode) {
            return {
                status: 0,
                message: "fail"
            };
        }

        let seller = await this.app.mysql.get('wang_seller_user', {
            phoneNumber
        });


        if (seller) {
            return {
                status: 2,
                seller,
                message: "ok"
            };
        } else {
            return {
                status: 1,
                message: "ok"
            };
       }

    }




    async getStayDetail(id) {

        var service = await this.app.mysql.select("stay_sell_service", {
             where: {  sell_id: id } 
        })

          var infos = await this.app.mysql.select("made_seller_infos", {
             where: {  sell_id: id } 
        })

       var pic = await this.app.mysql.select("made_seller_pic", {
                 where: {  sell_id: id } 
            })

        if (service&&infos) {
            return {
                status: 1,
                service,
                pic,
                infos
            }
        } else {
            return {
                status: 0
            }
        }
    }


   async getSellerNotice(sid) {
        var notices = await this.app.mysql.select("seller_notice", {
            where: { sid }
        })

        if (notices) {
            return {
                status: 1,
                notices
            }
        } else {
            return {
                status: 0
            }
        }
    }


 async getRecord(id,type) {

      var where = { seller_id:id }  
      if(type=="scan"){

        where = { seller_id:id,type:1 }  

     }else  if(type=="code") {
      
      where = { seller_id:id,type:2 } 
    
     }

        var records = await this.app.mysql.select("have_scan", {
            where
        })

        if (records) {
            return {
                status: 1,
                records
            }
        } else {
            return {
                status: 0
            }
        }
    }



    async getUpdateName(id) {
        var updateItem = await this.app.mysql.get("made_seller_update", {
            sell_id:id
        })

        if (updateItem) {
            return {
                status: 1,
                updateItem
            };
        } else {
            return {
                status: 0
            }

        }

    }


   async getAllGoods(id) {

        let goods = await this.app.mysql.select('made_goods', {
            where: { goods_id: id }
        });

         
        if (goods.length > 0) {
            for (var good of goods) {
                let gid = good.id
                let foods = await this.app.mysql.select("made_goods_item", { where: { parent_id: gid } })
                let updateInfo = await this.app.mysql.get("made_goods_update", { goods_id: gid  })
                 for (var food of foods) {
                    var fid = food.id
                    let infos = await this.app.mysql.select("made_goods_info", { where: { goods_id: fid } })
                    let tags = await this.app.mysql.select("made_goods_item_tag", { where: { food_id: fid } })
                    food.infos = infos
                    food.tags = tags
                }
                good.foods = foods
                if(updateInfo){

                  good.updateInfo = updateInfo

                 }
                 
            }
        }

        if (goods) {
            return {
                status: 1,
                goods 
            }
        } else {
            return {
                status: 0
            }
        }

    }

  async updateGoodsName(id, sid, originName, updateName) {
        var newGoods = await this.app.mysql.insert("made_goods_update", {
            goods_id: id,
            sell_id: sid,
            origin_name: originName,
            update_name: updateName,
            type: "goodsName"
        })

        if (newGoods.affectedRows == 1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }





 async deleteGoodsName(id) {
        var item = await this.app.mysql.delete("made_goods", {
          id
        })

     if (item.affectedRows==1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }



   async testOrderCode(code,seller_id,sellerType) {

        var sql = `select * from wang_user_order where seller_id=${seller_id} and substring(order_id,-6)=${code}`
 
        var order = await this.app.mysql.query(sql)

        if (order.length>0) {

         var order_id=order[0].order_id

          if (sellerType == "food") {
                var orderList = await this.app.mysql.select("wang_order_list", {
                    where: { order_id }
                })
            } else {
                var orderList = await this.app.mysql.select("wang_hotel_order_list", {
                    where: { order_id }
                })
            }

         order[0].orderList =  orderList

       
         var getScan = await this.app.mysql.get("have_scan", {
                order_id,
                seller_id,
                type:2
            })
        if(!getScan){

         var orderScan = await this.app.mysql.insert("have_scan", {
                order_id,
                seller_id,
                type:2
            })
         }
         
        return {
              status: 1,
               order
            }
        } else {
            return {
                status: 0
            }
        }
    }



    async deleteGoodsItem(id) {
        var item = await this.app.mysql.delete("made_goods_item", {
            id
        })

        await this.app.mysql.delete("made_goods_item_tag", {
            food_id: id
        })
        await this.app.mysql.delete("made_goods_info", {
            goods_id: id
        })

        if (item.affectedRows == 1) {
            return {
                status: 1

            }
        } else {
            return {
                status: 0
            }
        }
    }



    async getSelfActivity(sid) {
       var sql = `select a.* ,d.content,s.gid,s.discount_price,s.count,s.new_user,s.begin_time,s.end_time from seller_join_activity s join seller_activity a on s.aid = a.id join seller_activity_detail d on a.id=d.activity_id  where s.sell_id=${sid}`
         var activitys = await this.app.mysql.query(sql)
        if (activitys) {
            return {
                status: 1,
                activitys
            }
        } else {
            return {
                status: 0
            }
        }
    }


    async  getGoodsItem(sid, type) {

        var where = { goods_id: sid }

        if (type == "wait") {
            where = { goods_id: sid, status: 0 }
        }

        if (type == "ok") {
            where = { goods_id: sid, status: 1 }
        }

        if (type == "off") {
            where = { goods_id: sid, status: -1 }
        }

        var goodsList = await this.app.mysql.select("made_goods", {
            where
        })

        if (goodsList.length > 0) {
            for (var i = 0; i < goodsList.length; i++) {
                var goods = goodsList[i]
                var gid = goods.id
                var goodsItem = await this.app.mysql.select("made_goods_item", {
                    where: { parent_id: gid }
                })
                goods.item = goodsItem
            }
        }

        if (goodsList) {
            return {
                status: 1,
                goodsList
            }
        } else {
            return {
                status: 0
            }
        }

    }




async  addNewOtherGoodsItem(name, goodsLogo, description, orderPrice, price, type, consumeValue, comeValue, bedSize, backValue, otherValue, basicServer,sellerType,goods,updateFlag,gid) {


    if(type==0){
        var newGoodsName = await this.app.mysql.insert("made_goods", {
            goods_id: goods.sell_id,
            name: goods.name,
            avatar: goods.avatar,
            type:0
        })
        type = newGoodsName.insertId

      }
      
     


        const conn = await this.app.mysql.beginTransaction();

        try {

         if(updateFlag==1){
           await this.app.mysql.delete("made_goods_item", {
            id:gid
        })
        await this.app.mysql.delete("made_goods_info", {
            goods_id: gid
        })
        await this.app.mysql.delete("made_goods_item_tag", {
            food_id: gid,
        })


        }

            var newItem = await this.app.mysql.insert("made_goods_item", {
                parent_id: type,
                seller_id:goods.sell_id,
                name,
                price: orderPrice,
                oldPrice: price,
                description,
                icon: goodsLogo
            })

            var goods_id = newItem.insertId
             
            await this.app.mysql.insert("made_goods_info", {
                goods_id,
                info: consumeValue,
                type: 2
            })

           if(sellerType=="stay"){
            await this.app.mysql.insert("made_goods_info", {
                goods_id,
                info: comeValue,
                type: 3
            })

            }

            await this.app.mysql.insert("made_goods_info", {
                goods_id,
                info: `${backValue}`,
                type: 1
            })
           if(sellerType=="stay"){
            for (var i = 0; i < basicServer.length; i++) {
                var tag = basicServer[i]
                await this.app.mysql.insert("made_goods_item_tag", {
                    food_id: goods_id,
                    tag,
                    type:i+1
                })
            }
           }
            if (otherValue != '') {
                await this.app.mysql.insert("made_goods_info", {
                    goods_id,
                    info: `${otherValue}`,
                    type: 4
                })
            }
          if(sellerType=="stay"){
            await this.app.mysql.insert("made_goods_item_tag", {
                food_id: goods_id,
                tag: `${bedSize}`,
                type: 5
            })
           }
            await conn.commit();
            return {
                status: 1
            }
        } catch (err) {

            await conn.rollback();
            throw err;
            return {
                status: 0
            }
        }
    }





  async addGoodsTeam(teamName, sell_id, goodsLogo,type) {
        var newGoods = await this.app.mysql.insert("made_goods", {
            goods_id: sell_id,
            name: teamName,
            avatar: goodsLogo,
            type
        })

     if (newGoods.affectedRows == 1) {

       await this.app.mysql.insert("made_goods_update", {
            sell_id,
            goods_id:newGoods.insertId,
            update_name: teamName,
            origin_name: teamName,
            status:-1,
            type:"goodsName"
        })

         return {
                status: 1,
                 gid:newGoods.insertId
            }
        } else {
            return {
                status: 0
            }
        }
    }



    async addSellerJoinActivity(newUser, count, discountPrice, gid, sell_id, beginTime, endTime,aid,sell_name) {

        var newActivity = await this.app.mysql.insert("seller_join_activity", {
            aid,
            sell_name,
            count,
            new_user: newUser,
            discount_price: discountPrice,
            sell_id,
            gid,
            begin_time: beginTime,
            end_time: endTime
        })

        if (newActivity.affectedRows == 1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }




    async goodsOffLine(goods_id) {
        var updateGoods = await this.app.mysql.update("made_goods", {
            status: -1
        }, {
                where: {
                    id: goods_id
                }
            })

        if (updateGoods.affectedRows == 1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }




    async getSellGoodsMessage(id) {

        let goods = await this.app.mysql.select('made_goods', {
            where: { goods_id: id }
        });

         
        if (goods.length > 0) {
            for (var good of goods) {
                let gid = good.id
                let foods = await this.app.mysql.select("made_goods_item", { where: { parent_id: gid } })
                 for (var food of foods) {
                    var fid = food.id
                    let infos = await this.app.mysql.select("made_goods_info", { where: { goods_id: fid } })
                    let tags = await this.app.mysql.select("made_goods_item_tag", { where: { food_id: fid } })
                    food.infos = infos
                    food.tags = tags
                }
                good.foods = foods
            }
        }


        if (goods) {
            return {
                status: 1,
                goods,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }
        }

    }



    async  addNewGoodsItem(name, goodsLogo, description, orderPrice, price, type,goods,updateFlag,fid) {


     if(type==0){
          var newGoods = await this.app.mysql.insert("made_goods", {
            goods_id: goods.sell_id,
            name: goods.name,
            avatar: goods.avatar,
            type:0
        })
        type = newGoods.insertId

      }

     if(updateFlag==1){

      var updateGoods = await this.app.mysql.update("made_goods_item", {
            parent_id: type,
            name,
            price: orderPrice,
            oldPrice: price,
            description,
            icon: goodsLogo,
            status: 0,
            ratings:0,
            sellCount:0
        },{where:{id:fid}})
        
        if (updateGoods.affectedRows == 1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }

     
    }else{

    var newGoods = await this.app.mysql.insert("made_goods_item", {
            parent_id: type,
            seller_id:goods.sell_id,
            name,
            price: orderPrice,
            oldPrice: price,
            description,
            icon: goodsLogo
        })

        if (newGoods.affectedRows == 1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }

     }
       
     
    }


    async  addGoodsItem(sell_id, teamName, goodsLogo, selectedFood) {

        var newGoods = await this.app.mysql.insert("made_goods", {
            goods_id: sell_id,
            name: teamName,
            avatar: goodsLogo
        })
        if (newGoods.affectedRows == 1) {
            var goodsId = newGoods.insertId

         await this.app.mysql.insert("made_goods_update", {
            sell_id,
            goods_id:goodsId,
            update_name: teamName,
            origin_name: teamName,
            status:-1,
            type:"goodsName"
        })

          if(selectedFood.length>0){ 
            for (var i = 0; i < selectedFood.length; i++) {
                var food = selectedFood[i]
                var newGoodsItem = await this.app.mysql.insert("made_goods_item", {
                    seller_id: sell_id,
                    parent_id: goodsId,
                    name: food.name,
                    type: food.type,
                    price: food.price,
                    oldPrice: food.oldPrice,
                    description: food.description,
                    icon: food.icon,
                    stock: food.stock,
                    status: food.status
                })
            }
          }  
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }

    }





    async sellerAggreeDocument(id,type) {
         

     if(type=="coupon"){

         var updateSeller = await this.app.mysql.update("seller_auth_name", {
            couponStatus: 1
        }, { where: { sell_id: id } })

     }else{

    var updateSeller = await this.app.mysql.update("seller_auth_name", {
            orderStatus: 1
        }, { where: { sell_id: id } })
    }

     if (updateSeller.affectedRows==1) {

        let seller = await this.app.mysql.get("made_test_sellers", {
                 id
               })

        var addNewSeller = await this.app.mysql.insert("made_sellers", {
            id: seller.id,
            seller_id: seller.seller_id,
            name: seller.name,
            type: seller.type,
            childType: seller.childType,
            avatar: seller.avatar,
            province: seller.province,
            bulletin: seller.bulletin,
            cityName: seller.cityName,
            maxPrice: seller.maxPrice,
            maxDistance: seller.maxDistance,
            maxPeople: seller.maxPeople,
            minPrice: seller.minPrice,
            invoice: seller.invoice,
            invoiceText: seller.invoiceText,
            send: seller.send,
            cancelTime: seller.cancelTime,
            stock: seller.stock,
            description: seller.description,
            deliveryPrice: seller.deliveryPrice,
            deliveryTime: seller.deliveryTime,
            district: seller.district,
            sellTime: seller.sellTime,
            sellType: seller.sellType,
            serverPhone: seller.serverPhone,
            traffic: seller.traffic,
            addressDetail: seller.addressDetail,
            city: seller.city,
            lat: seller.lat,
            lng: seller.lng,
            on_sell: -1
        })


        return {
                status: 1,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }

        }

    }




     async addSellerDetailInfo(sell_id, wifi, piao, pack, distanceNumber, peopleStatus, peopleNumber, distanceStatus,couponStatus,couponText,send,moneyText, feeNumber, feeStatus,sellerType,orderText,bulletin) {


        const conn = await this.app.mysql.beginTransaction();

        try {
             

            await this.app.mysql.delete("made_seller_infos", {
                sell_id
            })
            
         if(wifi==1){
            await this.app.mysql.insert("made_seller_infos", {
                sell_id,
                info: "支持wifi",
                status: 1
            })

           }

          if(piao==1){
            await this.app.mysql.insert("made_seller_infos", {
                sell_id,
                info: "支持发票",
                status: 1
            })
           }

     
        if(pack==1){
            await this.app.mysql.insert("made_seller_infos", {
                sell_id,
                info: "支持免费打包",
                status: pack
            })
          }
         if(send==1){
            await this.app.mysql.insert("made_seller_infos", {
                sell_id,
                info: "支持接送",
                status: 1
            })
          }
            if (distanceStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    maxDistance: distanceNumber
                }, {
                        where: { id: sell_id }
                    })
            }else{ 

               await this.app.mysql.update("made_sellers", {
                     maxPeople:10000000000
                }, {
                        where: { id: sell_id }
                    })

             }

            if (peopleStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    maxPeople: peopleNumber
                }, {
                        where: { id: sell_id }
                    })
            }else{ 

               await this.app.mysql.update("made_sellers", {
                     maxPeople:10000
                }, {
                        where: { id: sell_id }
                    })

             }


              await this.app.mysql.update("made_sellers", {
                     bulletin:bulletin
                }, {
                        where: { id: sell_id }
                    })



             if (couponStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    invoice:1,
                    invoiceText:couponText
                }, {
                        where: { id: sell_id }
                    })
            }else{ 

               await this.app.mysql.update("made_sellers", {
                    invoice:0,
                    invoiceText:""
                }, {
                        where: { id: sell_id }
                    })

             }

            if (feeStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    deliveryPrice: feeNumber
                }, {
                        where: { id: sell_id }
                    })
            } else {
                await this.app.mysql.update("made_sellers", {
                    deliveryPrice: 0
                }, {
                        where: { id: sell_id }
                    })
            }


          if(sellerType=="food"){
             await this.app.mysql.update("made_sellers", {
                   cancelTime:moneyText
                }, {
                        where: { id: sell_id }
                    })

                }

           if(sellerType=="food"){
             await this.app.mysql.update("made_sellers", {
                  deliveryTime:orderText
                }, {
                        where: { id: sell_id }
                    })

                }




            await conn.commit(); 
            return {
                status:1
            }
        } catch (err) {
        
            await conn.rollback(); 
            throw err;
             return {
                status:0
            }
        }

    }



    async addSellerServer(sell_id, send,pass,bed,allTime,distanceNumber, peopleStatus, peopleNumber, distanceStatus, area, cookCount, pNumber, bedNumber, otherServer, basicServer, showerServer, cookServer, homeServer, inType, inTime, leftTime, supportReturn, noReturn,couponStatus,couponText,bulletin) {
     

       const conn = await this.app.mysql.beginTransaction();

        try {

          var del =   await this.app.mysql.delete("stay_sell_service", {
                sell_id
            })

               await this.app.mysql.update("made_sellers", {
                     bulletin:bulletin
                }, {
                        where: { id: sell_id }
                    })



            if (distanceStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    maxDistance: distanceNumber
                }, {
                        where: { id: sell_id }
                    })
            }else{ 

               await this.app.mysql.update("made_sellers", {
                     maxPeople:10000000000
                }, {
                        where: { id: sell_id }
                    })

             }

            if (peopleStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    maxPeople: peopleNumber
                }, {
                        where: { id: sell_id }
                    })
            }else{ 

               await this.app.mysql.update("made_sellers", {
                     maxPeople:10000
                }, {
                        where: { id: sell_id }
                    })

             }


           if (couponStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    invoice:1,
                    invoiceText:couponText
                }, {
                        where: { id: sell_id }
                    })
            }else{ 

               await this.app.mysql.update("made_sellers", {
                    invoice:0,
                    invoiceText:""
                }, {
                        where: { id: sell_id }
                    })

             }

            if (area && area != '') {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: "1",
                    info: area,
                    type: 0
                })

            }

            if (cookCount && cookCount != '') {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: "3",
                    info: cookCount,
                    type: 0
                })

            }

            if (pNumber && pNumber != 0) {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: 2,
                    info: `可住${pNumber}人`,
                    type: 0
                })

            }

            if (bedNumber && bedNumber != 0) {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: 4,
                    info: `一共${bedNumber}张床`,
                    type: 0
                })

            }



            for (let i=0; i<basicServer.length;i++) {
                 var item = basicServer[i]
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: item,
                    type: -1
                })
            }

            for (let i=0; i<otherServer.length;i++) {
                 var item = otherServer[i]
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: item,
                    type: 5
                })
            }


            if (send == 1) {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: "商家接送机",
                    type: 1
                })

            }

            if (pass == 1) {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: "有停车位",
                    type: 1
                })

            }

            if (bed == 1) {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: "床品每客更换",
                    type: 1
                })

            }

            if (inTime != '') {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: "入住时间",
                    info: inTime,
                    type: 6
                })

            }

            if (leftTime != '') {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: "退房时间",
                    info: leftTime,
                    type: 7
                })

            }

            if (supportReturn != '') {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: supportReturn,
                    info: "取消订单100%退款",
                    type: 8
                })

            }

            if (noReturn != '') {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: noReturn,
                    info: "取消订单不退款",
                    type: 9
                })

            }


            if (inType != '') {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info_title: "入住方式",
                    info: inType,
                    type: 1
                })

            }

            if (allTime == 1) {
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: "24小时入住",
                    type: 1
                })

            }



            for (let i=0; i<showerServer.length;i++) {
                var item = showerServer[i]
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: item,
                    type: 2
                })
            }


            for (let i=0; i<cookServer.length;i++) {
               var item = cookServer[i]
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: item,
                    type: 4
                })
            }

            for (let i=0; i<homeServer.length;i++) {
                var item = homeServer[i]
                await this.app.mysql.insert("stay_sell_service", {
                    sell_id,
                    info: item,
                    type: 2
                })
            }

            if (distanceStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    maxDistance: distanceNumber
                }, {
                        where: { id: sell_id }
                    })
            }

            if (peopleStatus == 1) {
                await this.app.mysql.update("made_sellers", {
                    maxPeople: peopleNumber
                }, {
                        where: { id: sell_id }
                    })
            }

            await conn.commit();

            return {
                status: 1
            }

        }catch (err) {
            await conn.rollback();
            throw err;
            return {
                status: 0
            }
        }
        

    }



    async getAuthSeller(id) {
     
        var seller = await this.app.mysql.get("seller_auth_name", {
            sell_id:id
        })


        var info = await this.app.mysql.get("seller_auth_infos", {
            sell_id:id
        })


        if (seller) {
            return {
                status: 1,
                seller,
                info,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }

        }

    }


   async sellerAuthName(sell_id, encode, phone, code, seller_name, id_code, credit_card) {

        let decode = sha1(code + phone + "wang_user")

        if (decode != encode) {
            return {
                status: 0,
                message: "fail"
            };
        }

        let getSeller = await this.app.mysql.get('seller_auth_name', {
            sell_id
        });


        if (getSeller) {
            return {
                status: 2,
                message: "ok",
                id: getSeller.sell_id
            };
        } else {
            var addSeller = await this.app.mysql.insert("seller_auth_name", {
                sell_id,
                phone,
                seller_name,
                orderStatus:0,
                couponStatus:0,
                id_code,
                credit_card
            })
            if (addSeller.affectedRows == 1) {
                return {
                    status: 1,
                    message: "ok",
                    id: addSeller.insertId
        
                };
            } else {
                return {
                    status: 0
                }

            }
        }

    }
            











     async getSellerActivity(time,type) {


      
     var sql = `select s.*,d.content from seller_activity s join seller_activity_detail d on s.id = d.activity_id where time>${time} and  s.type='${type}'`
       
 
        var activitys = await this.app.mysql.query(sql)

        if (activitys) {
            return {
                status: 1,
                activitys
            }
        } else {
            return {
                status: 0
            }
        
       }
     }


            
  async getDateSellMessage(sid, startTime, endTime, beforeStartTime, beforeEndTime) {

        var orderSql = `select * from wang_user_order where seller_id=${sid} and orderTime>=${startTime} and  orderTime<=${endTime}`
        var orderMessage = await this.app.mysql.query(orderSql)

        var rateSql = `select *  from made_seller_comments where seller_id=${sid} and rateTime>=${startTime} and  rateTime<=${endTime}`
        var rateMessage = await this.app.mysql.query(rateSql)

        var viewSql = `select sum(view) as count  from made_seller_view where seller_id=${sid} and viewTime>=${startTime} and  viewTime<=${endTime} `
        var viewMessage = await this.app.mysql.query(viewSql)

        var oldUser = 0
        var newUser = 0
        if (orderMessage.length > 0) {
            for (let i = 0; i < orderMessage.length; i++) {
                let order = orderMessage[i]
                let uid = order.user_id
                let userSql = `select * from wang_user_order where seller_id=${sid} and user_id=${uid}  and (orderTime<${startTime} or orderTime>${endTime})  `
                let userMessage = await this.app.mysql.query(userSql)
                if (userMessage.length > 0) {
                    oldUser++
                } else {
                    newUser++
                }
            }
        }

        var beforeOrderSql = `select * from wang_user_order where seller_id=${sid} and orderTime>=${beforeStartTime} and  orderTime<=${beforeEndTime}`
        var beforeOrderMessage = await this.app.mysql.query(beforeOrderSql)

        var beforeRateSql = `select *  from made_seller_comments where seller_id=${sid} and rateTime>=${beforeStartTime} and  rateTime<=${beforeEndTime}`
        var beforeRateMessage = await this.app.mysql.query(beforeRateSql)

        var oldUserBefore = 0
        var newUserBefore = 0
        if (beforeOrderMessage.length > 0) {
            for (let i = 0; i < beforeOrderMessage.length; i++) {
                let order = beforeOrderMessage[i]
                let uid = order.user_id
                let userSql = `select * from wang_user_order where seller_id=${sid} and user_id=${uid}  and (orderTime<${beforeStartTime} or orderTime>${beforeEndTime})  `
                let userMessage = await this.app.mysql.query(userSql)
                if (userMessage.length > 0) {
                    oldUserBefore++
                } else {
                    newUserBefore++
                }
            }
        }


        if (orderMessage&&beforeOrderMessage) {
            return {
                status: 1,
                orderMessage,
                rateMessage,
                beforeOrderMessage,
                beforeRateMessage,
                oldUser,
                newUser,
                oldUserBefore,
                newUserBefore,
                viewMessage
            }
        } else {
            return {
                status: 0
            }
        }
    }




   async updateSellerDiscountStatus(cid) {

        var updateDiscount = await this.app.mysql.update("made_seller_coupon", {
            status: 2
        }, { where: { id: cid } })

        if (updateDiscount.affectedRows==1) {
            return {
                status: 1
               
            }
        } else {
            return {
                status: 0
            }
        }
    }









   async updateSellDiscount(sid, price, discount, type, update, start_time, end_time, description) {

        if (update == -1) {

            var coupons = await this.app.mysql.insert("made_seller_coupon", {
                sell_id: sid,
                price,
                type,
                discount,
                coupon_title:description,
                begin_time:start_time,
                end_time,
                status: 0
            })


        } else {

            var coupons = await this.app.mysql.update("made_seller_coupon", {
                sell_id: sid,
                price,
                type,
                discount,
                coupon_title:description,
                end_time,
                begin_time:start_time,
                status: 0
            }, {
                    where: {
                        id: update
                    }
                })

        }

        if (coupons.affectedRows == 1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }







         async sellerPunish(sid) {
        var punishs = await this.app.mysql.select("seller_punish", {
            where: { sid }
        })

        if (punishs) {
            return {
                status: 1,
                punishs
            }
        } else {
            return {
                status: 0
            }
        }
    }



   async getSellerCoupon(id, status) {

        var where = { sell_id: id }

        if (status == 0) {
            where = { sell_id: id, status: 0 }
        }

        if (status == 1) {
            where = { sell_id: id, status: 1 }
        }

        if (status == 2) {
            where = { sell_id: id, status: 2 }
        }



        var coupons = await this.app.mysql.select("made_seller_coupon", {
            where
        })

        if (coupons) {
            return {
                status: 1,
                coupons
            }
        } else {
            return {
                status: 0
            }
        }
    }



     async addReply(cid, content) {

        var newReply = await this.app.mysql.insert("made_seller_reply", {
            comment_id: cid,
            content
        })

        var updateComment = await this.app.mysql.update("made_seller_comments", {
            status: 2
        },{
             where:{ id: cid}
         })
        if (newReply.affectedRows == 1 && updateComment.affectedRows == 1) {
            return {
                mrssage: "ok",
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }

    }



    async sellerPsLogin(username,password){

        var seller = await this.app.mysql.get("wang_seller_user", {
            username,
            password
           })
  
        if (seller) {
            return {
                status: 1,
                message: "ok",
                seller  
            };
        } else {
            return {
                status: 0
            }

        }

    }
       



    async getSellerRating(sid, sellerType, type, start) {

        var sum = start * 15

        var nextStart = -1

        var commentSql = `select * from made_seller_comments where seller_id=${sid} limit ${sum}`


        if (type == "good") {
            commentSql = `select * from made_seller_comments where seller_id=${sid} and score>=4 limit ${sum}`
        } else if (type == "middle") {
            commentSql = `select * from made_seller_comments where seller_id=${sid} and score<4 and score>=3 limit ${sum}`
        } else if (type == "low") {
            commentSql = `select * from made_seller_comments where seller_id=${sid} and score<4 and score<3 limit ${sum}`
        }


        var ratings = await this.app.mysql.query(commentSql)
        var  goodsCount = 0
        var   count = 0
        if (ratings.length > 0) {
            if (ratings.length == sum) {
                nextStart = start + 1
            }
            var sql = `select count(1) as count from wang_user_order where seller_id=${sid} group by user_id`
            var consumer = await this.app.mysql.query(sql);
           count = consumer[0].count
            if (sellerType == "food") {
                var goodsSql = `select count(1) as count from made_goods_item where seller_id=${sid}`
                var goodsCount = await this.app.mysql.query(goodsSql);
               goodsCount = goodsCount[0].count
            }

            for (var i = 0; i < ratings.length; i++) {
                var rating = ratings[i]
                var rid = rating.id
                 var order_id = rating.order_id
                var ratingPic = await this.app.mysql.select("seller_comment_pic", {
                    where: { comment_id: rid }
                })
              
               if (sellerType == "food") {
                    var goods = await this.app.mysql.select("wang_order_list", {
                        where: { order_id }
                    })
                    rating.goods = goods
                }
                if (rating.status == 2) {
                    var sellerReply = await this.app.mysql.get("made_seller_reply", {
                        comment_id: rid 
                    })
                    rating.reply = sellerReply 
                }
                rating.pics = ratingPic
               

            }
        }

        if (ratings) {
            return {
                status: 1,
                goodsCount,
                 count, 
                nextStart,
                ratings
            }
        } else {
            return {
                status: 0
            }
        }
    }




     
    async getSale(sid) {
        var saleInfo = await this.app.mysql.get("seller_sales", {
            sell_id: sid
        })

        if(saleInfo){
        var sale_id = saleInfo.sale_id

        var sale = await this.app.mysql.get("sales", {
            id: sale_id
        })

        if (sale) {
            return {
                status: 1,
                sale
            }
        } else {
            return {
                status: 0
            }
        }
     }else{

 return {
                status: 0
            }

       }
    
    }




     async addFeedBack(text, type, phone, imgs,postTime) {

        

        var newFeedBack = await this.app.mysql.insert("seller_feedback", {
            phone,
            text,
            type,
            postTime
        })


        if (imgs.length > 0) {
            var fid = newFeedBack.insertId
            for (var i = 0; i < imgs.length; i++) {
                var img = imgs[i]
                var addPic = await this.app.mysql.insert("seller_feedback_pic", {
                    fid,
                    url: img
                })
            }

        }

        if (newFeedBack.affectedRows==1) {
            return {
                mrssage: "ok",
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }

    }





       async updateSellerSend(sid) {
        var updateSend = await this.app.mysql.update("made_sellers", {
           send:2,
        },{
            where:{
                id:sid
            }
        })
        if (updateSend.affectedRows==1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }

     async getSellerAuthDetail(sid) {
        var details = await this.app.mysql.get("seller_auth_infos", {
           sell_id: sid
        })

          var pics = await this.app.mysql.select("seller_auth_pic", {
            where: { sell_id: sid }
        })

        if (details) {
            return {
                status: 1,
                details,
                pics
            }
        } else {
            return {
                status: 0
            }
        }
    }

     
    async updateSellerImg(sid, imgList, imgList1) {

        var delImg = await this.app.mysql.delete("made_seller_pic", {
            sell_id: sid
        })

      if(imgList.length>0){

          for(var i=0;i<imgList.length;i++) {
            var img = imgList[i]
            var newSurface = await this.app.mysql.insert("made_seller_pic", {
                sell_id: sid,
                type: "surface",
                url: img
            })
        }
 

        }

       
     if(imgList1.length>0){
        for (var i=0;i<imgList1.length;i++) {
            var img = imgList1[i]
            var newInner = await this.app.mysql.insert("made_seller_pic", {
                sell_id: sid,
                type: "inner",
                url: img
            })
        }

     }


        if (delImg && newSurface && newInner) {
            return {
                mrssage: "ok",
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }

    }

      
        
  async deleteSellCoupon(id) {
        var coupons = await this.app.mysql.delete("made_seller_supports", {
            id
        })
        if (coupons.affectedRows==1) {
            return {
                status: 1
                
            }
        } else {
            return {
                status: 0
            }
        }
    }


   

  async getSellerImg(id) {
        var imgs = await this.app.mysql.select("made_seller_pic", {
            where: { sell_id: id }
        })
        if (imgs) {
            return {
                status: 1,
                imgs
            }
        } else {
            return {
                status: 0
            }
        }
    }



   async updateSellCoupon(sid, price, discount, type, update,description,end_time) {

        if (update == -1) {
             
                var coupons = await this.app.mysql.insert("made_seller_supports", {
                    sell_id: sid,
                    price,
                    type,
                    discount,
                    description,
                    end_time
                })
            

        } else {

                var coupons = await this.app.mysql.update("made_seller_supports", {
                    sell_id: sid,
                    price,
                    type,
                    discount,
                    description,
                    end_time
                }, {
                        where: {
                            id: update
                        }
                    })
           
        }

        if (coupons.affectedRows==1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }


        async getSellCoupon(sid) {
        var coupons = await this.app.mysql.select("made_seller_supports", {
            where: { sell_id: sid }
        })
        if (coupons) {
            return {
                status: 1,
                coupons
            }
        } else {
            return {
                status: 0
            }
        }
    }
       

        async changeConsumerType(sid, type) {
        var newType = await this.app.mysql.update("made_sellers", {
           sellType:type
        },
            {
                where: { id: sid }
            })

        if (newType.affectedRows == 1) {
            return {
                status: 1,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }

        }
    }


      async updateChildType(sid,update_type,origin_type){
       
        var newType = await this.app.mysql.insert("made_seller_update", {
            sell_id:sid,
            type:"type",
            update:update_type,
            origin:origin_type
        })

        if (newType.affectedRows == 1) {
            return {
                status: 1,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }

        }
   }

       
     
    async  updateSellTime(sid, beginTime, endTime) {

        var sellTime = beginTime + "-" + endTime

        var newTime = await this.app.mysql.update("made_sellers", {
            sellTime
        },
            {
                where: { id: sid }
            })

        if (newTime.affectedRows == 1) {
            return {
                status: 1,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }

        }
    }



      async getSeller(id) {

        var information = await this.app.mysql.get("made_sellers", {
            seller_id: id
        })

       if (!information) {
            information = await this.app.mysql.get("made_test_sellers", {
                seller_id: id,
            })
           if(information.status==-1){

             information.test=1

            }else if(information.status==0){
            
             information.test=2
            
           }

          
        }

        if (information) {
            return {
                status: 1,
                seller: information
            }
        } else {
            return {
                status: 0
            }
        }
    }

    
    async takeOrder(order_id,status) {
        var updateOrder = await this.app.mysql.update("wang_user_order", {
            status
        },{where:{order_id}})
        if (updateOrder.affectedRows==1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }



    
    async changeSellerStatus(sid,status){

   var updateSeller = await this.app.mysql.update("made_sellers", {
           on_sell:status
        },
       {where:{id:sid}
        })
        if (updateSeller.affectedRows==1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }






    async updateAddress(sid,addressInfo, selProvince, address, selCity, selDistrict) {

        var newAddress = await this.app.mysql.update("made_sellers", {
            province: selProvince,
            cityName:selCity,
            district: selDistrict,
            addressDetail:address,
            city:addressInfo.adcode,
            traffic:addressInfo.traffic,
            lat:addressInfo.lat,
            lng:addressInfo.lng
        },
            {
                where: { id: sid }
            })

        if (newAddress.affectedRows == 1) {
            return {
                status: 1,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }

        }


    }





  async haveConsumer(order_id,consumer_time) {
        var updateOrder = await this.app.mysql.update("wang_user_order", {
            have_consumer:1,
            consumer_time,
            status:3
        },{where:{order_id}})
        if (updateOrder.affectedRows==1) {
            return {
                status: 1
            }
        } else {
            return {
                status: 0
            }
        }
    }




    async updateSellerName(id,origin_name,update_name){
       
        var updateSeller = await this.app.mysql.insert("made_seller_update", {
            sell_id:id,
            origin_name:origin_name,
            update_name:update_name,
            type:"name"
           })
  
        if (updateSeller.affectedRows==1) {
            return {
                status: 1,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }

        }

    }




      async updateSellerPhone(id,phone){
        console.log(id,name)
        var updateSeller = await this.app.mysql.update("made_sellers", {
            id,
            serverPhone:phone
           })
  
        if (updateSeller.affectedRows==1) {
            return {
                status: 1,
                message: "ok"
            };
        } else {
            return {
                status: 0
            }

        }

    }




async saveSeller(sid,name, licentCode, licenName, sellerName, address, phoneNumber, province, city, status, district, businessLicence, idFront, idBack, linkMan, textCode, idCode, encode, uid, adcode,lat,
lng,type) {

        let decode = sha1(textCode + phoneNumber + "wang_user")


        if (decode != encode) {
            return {
                mrssage: "fail",
                status: -1
            }
        }

        var sellerTest = await this.app.mysql.select("made_test_sellers", {
            where: {seller_id: uid,on_sell:-1 }
        })

        if (sellerTest.length > 0) {
            return {
                mrssage: "fail",
                status: 2
            }
        }
        var avatar = "https://media.kaolaplay.com/asas.jpg"
       
         var addressInfo = province+city+district
       
        var addressDetail = address.substr(addressInfo.length)


       var delPic = await this.app.mysql.delete("seller_auth_pic", {
            sell_id:sid
        })
        var delSeller = await this.app.mysql.delete("made_test_sellers", {
            seller_id:uid
        })
       var delDetail = await this.app.mysql.delete("seller_auth_infos", {
            sell_id:sid
        })
      

        var addNewSeller = await this.app.mysql.insert("made_test_sellers", {
            seller_id: uid,
            name,
            type,
            childType: "",
            avatar,
            on_sell: status,
            province: province,
            cityName: city,
            district: district,
            serverPhone: phoneNumber,
            traffic: address,
            addressDetail,
            city:adcode,

   
         lat,

            lng,
            on_sell:-1
        })
        var sell_id = addNewSeller.insertId


        var authSeller = await this.app.mysql.insert("seller_auth_infos", {
            sell_id,
            sell_code: licentCode,
            licent_name:licenName,
            owner_name:sellerName,
            manager_name:linkMan,
            id_code:idCode,
            manager_phone:phoneNumber,
            address,
        })

        var newBusinessLicence = await this.app.mysql.insert("seller_auth_pic", {
            sell_id,
            url: businessLicence,
            type: "businessLicence"
        })

        var newIdFront = await this.app.mysql.insert("seller_auth_pic", {
            sell_id,
            url: idFront,
            type: "idFront"
        })

        var newIdBack = await this.app.mysql.insert("seller_auth_pic", {
            sell_id,
            url: idBack,
            type: "idBack"
        })



        if (authSeller.affectedRows == 1&&addNewSeller.affectedRows == 1) {
            return {
                message: "ok",
                status: 1
            }

        } else {
            return {
                message: "fail",
                status: 0
            }
        }
    }



   






   async  updateInfoByPs(newPs, oldPs, sid) {

             

        let getSeller = await this.app.mysql.get('wang_seller_user', {
            password:oldPs,
            id:sid
        });

         if (getSeller) {
            var updateUser = await this.app.mysql.update("wang_seller_user", {
                password: newPs
            },
                {
                    where: { id: sid }
                })

            if (updateUser.affectedRows == 1) {
                return {
                    status: 1,
                    message: "ok"
                };
            } else {
                return {
                    status: 0
                }

            }

        } else {
            return {
                status: 0
            }
        }
        
        
        
    }


    async updateInfoByPhone(encode, phoneNumber, code,sid) {
     

        let decode = sha1(code + phoneNumber + "wang_user")
         
        if (decode != encode) {
            return {
                status: 0,
                message: "fail"
            };
        }

        let getSeller = await this.app.mysql.get('wang_seller_user', {
            phoneNumber
        });


        if (getSeller) {
            return {
                status: 2,
                message: "ok"
            };
        } else {
            var updateSeller = await this.app.mysql.update("wang_seller_user", {
                id:sid,
                phoneNumber
            })
            if (updateSeller.affectedRows == 1) {
                return {
                    status: 1,
                    message: "ok"
                };
            } else {
                return {
                    status: 0
                }

            }
        }

    }











     async sellerAuthRegister(encode,phoneNumber,code,username,password,type) {
     
        let decode = sha1(code + phoneNumber + "wang_user")

        if(decode!=encode){
             return {
                status: 0,
                message: "fail"
            };
        }

        let seller = await this.app.mysql.get('wang_seller_user', {
           phoneNumber
        });


        if (seller) {
            return {
                status: 2,
                message: "ok",
                id:seller.id,
                u_id:seller.u_id,
                type:seller.type
            };
        } else {
          
            var avatar = "http://oxszj0so4.bkt.clouddn.com/user.png"
            var addSeller = await this.app.mysql.insert("wang_seller_user", {
                type,
                u_id: decode,
                password,
                username,
                phoneNumber,
                avatar
            })
            if (addSeller.affectedRows == 1) {
                return {
                    status: 1,
                    message: "ok",
                    id:addSeller.insertId,
                    u_id:decode,
                    type

                };
            } else {
                return {
                    status: 0
                }

            }
        }

    }





 async getInformation(id) {



        var information = await this.app.mysql.get("wang_user", {
            u_id: id,
        })
        if (information) {
            return {
                status: 1,
                user: information
            }
        } else {
            return {
                status: 0
            }
        }
    }


  async getSellerNewOrder(id) {
        var orders = await this.app.mysql.select("wang_user_order", {
            where: {
                seller_id: id,
                status: 1
            }
        })
        if (orders) {
            return {
                status: 1,
                orders
            }
        } else {
            return {
                status: 0
            }
        }
    }

 async  learnDetail(id) {
         
          let details = await this.app.mysql.get('seller_learn_detail', {
           learn_id:id
        });

        if (details) {
            return {
                status: 1,
                details,
                message:"ok",
            };
        } else {
            return {
                status: 1,
                message:"获取数据失败",
            };
        }
    }


   async getTodayMessage(sid,beginTime,endTime) {
          let sqlQuery = `select count(1) as sellCount,sum(sumPrice) as sumPrice,seller_id from  wang_user_order where seller_id=${sid} and orderTime>=${beginTime} and orderTime<=${endTime}`
          var message = await this.app.mysql.query(sqlQuery);

          let commentQuery = `select count(1) as comments from  made_seller_comments where seller_id=${sid} and rateTime>=${beginTime} and rateTime<=${endTime}`
          let comments= await this.app.mysql.query(commentQuery);
      

      if(message[0].seller_id){

         message  = message[0]

        if(comments[0].comments){
          message.comments = comments[0].comments
         }else{
         message.comments = 0
        }
         
         

      }else{

       message = {
           sellCount:0,
           sumPrice:0,
           seller_id:sid,
           comments:0
       }

      }

        if (message) {
            return {
                status: 1,
                 message:message
            }
        } else {
            return {
                status: 0
            }
        }
    }



    async getSellerInfo(id) {
        var information = await this.app.mysql.get("wang_seller_user", {
            id: id,
        })
        if (information) {
            return {
                status: 1,
                seller: information
            }
        } else {
            return {
                status: 0
            }
        }
    }



    async getLearnHot() {
        var information = await this.app.mysql.select("seller_learn",{orders: [["haveLook", "desc"]]})
        if (information) {
            return {
                status: 1,
               learn: information
            }
        } else {
            return {
                status: 0
            }
        }
    }







    async getOrderById(order_id,seller_id,sellerType) {

        var order = await this.app.mysql.get("wang_user_order", {
                order_id,
                seller_id
        })

        if (order) {

          if (sellerType == "food") {
                var orderList = await this.app.mysql.select("wang_order_list", {
                    where: { order_id }
                })
            } else {
                var orderList = await this.app.mysql.select("wang_hotel_order_list", {
                    where: { order_id }
                })
            }


         order.orderList =  orderList


        var getScan = await this.app.mysql.get("have_scan", {
                order_id,
                seller_id,
                type:1
            })
        if(!getScan){

           var addScan = await this.app.mysql.insert("have_scan", {
                order_id,
                seller_id,
                type:1
            })

         }
         return {
                status: 1,
                order,
            }
        } else {
            return {
                status: 0
            }
        }
    }



async saveTripSeller(tripName, mobile, selProvince, selCity, selDistrict, address, tripType, minPrice, maxPrice, intro, imgList, businessLicence, idFront, idBack, linkMan, email, sellerPhone, textCode, uid, encode, avatar, addressInfo, type) {

        let decode = sha1(textCode + sellerPhone + "wang_user")
        if (decode != encode) {
            return {
                mrssage: "验证码不正确",
                status: -1
            }
        }

        var sellerTest = await this.app.mysql.select("wang_seller_user", {
            where: { u_id: uid }
        })

        if (sellerTest.length > 0) {
            return {
                mrssage: "fail",
                status: 2
            }
        }

        var newSeller = await this.app.mysql.insert("wang_seller_user", {
            u_id: uid,
            type,
            username: linkMan,
            avatar,
            phoneNumber: sellerPhone,
            role: 0,
            status: -2,
            email
        })
        var sell_id = newSeller.insertId

        var addNewSeller = await this.app.mysql.insert("made_sellers", {
            seller_id: sell_id,
            lat: addressInfo.lat,
            lng: addressInfo.lng,
            city: addressInfo.adcode,
            traffic: addressInfo.traffic,
            name: tripName,
            type,
            childType: tripType,
            description: intro,
            minPrice,
            maxPrice,
            avatar: imgList[0],
            on_sell: -1,
            province: selProvince,
            cityName: selCity,
            district: selDistrict,
            serverPhone: mobile,
            addressDetail: address
        })

        var newBusinessLicence = await this.app.mysql.insert("seller_auth_pic", {
            sell_id,
            url: businessLicence,
            type: "businessLicence"
        })

        var newIdFront = await this.app.mysql.insert("seller_auth_pic", {
            sell_id,
            url: idFront,
            type: "idFront"
        })

        var newIdBack = await this.app.mysql.insert("seller_auth_pic", {
            sell_id,
            url: idBack,
            type: "idBack"
        })

        for (let img of imgList) {
            var newIdBack = await this.app.mysql.insert("seller_auth_pic", {
                sell_id,
                url: img,
                type: "surface"
            })
        }




        if (newSeller.affectedRows == 1 && addNewSeller.affectedRows == 1) {
            return {
                message: "ok",
                status: 1
            }

        } else {
            return {
                message: "fail",
                status: 0
            }
        }
    }










      async getSellerOrder(sid, type, start,sellerType) {

        var PageSize = 15
        var count = PageSize * start
        var nextStart = -1

        if (type == "all") {
            var orders = await this.app.mysql.select("wang_user_order", {
                where: { seller_id: sid },
                orders: [["orderTime", "desc"]],
                limit: PageSize * start

            })
        } else {
              if (type == 4) {
                var orderSql = `select * from wang_user_order where (status=3 or status=4) and seller_id=${sid} order by orderTime desc limit  ${count}`
                var orders = await this.app.mysql.query(orderSql)
            } else {
             var orders = await this.app.mysql.select("wang_user_order", {
                where: { seller_id: sid, status: type },
                orders: [["orderTime", "desc"]],
                limit: count
            })
          }
        }

        if (orders.length == count) {
            nextStart = start + 1
        }


        if (orders) {

           for (var i = 0; i < orders.length; i++) {
                var order_id = orders[i].order_id
               if (sellerType == "food") {
                    var list = await this.app.mysql.select("wang_order_list", {
                        where: { order_id }
                    })
                } else {
                    var orderSql = `select * from wang_hotel_order_list h join made_goods_item g on h.goods_id=g.id where h.order_id='${order_id}'`
                    var list = await this.app.mysql.query(orderSql)
                }
                orders[i].orderList = list
            }


            return {
                status: 1,
                orders,
                message: "ok",
                nextStart
            }
        } else {
            return {
                status: 0
            }
        }


    }

}

module.exports = User;
